package com.cqc.jdk8.Jutil;

import org.junit.Assert;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;
import java.util.stream.Collectors;

/**
 * @author cui
 * @date 2024/8/31 15:40
 * @description
 **/
public class StringJoinerTest {

    @Test
    public void f1() {
        StringJoiner joiner = new StringJoiner("、");
        System.out.println(joiner.toString());
    }

    //没有前缀和后缀
    @Test
    public void test1(){
        StringJoiner joiner = new StringJoiner(",");
        String result = joiner.toString();
        System.out.println(result);
        //0
        System.out.println(result.length());
    }
    //有前缀和后缀
    @Test
    public void test2(){
        StringJoiner joiner = new StringJoiner(",","{","}");
        String result = joiner.toString();
        //{}  2
        System.out.println(result);
        System.out.println(result.length());
    }
    //有值+没有前缀和后缀
    @Test
    public void test3(){
        StringJoiner joiner = new StringJoiner(",");
        StringJoiner add = joiner.add("a").add("b").add("c").setEmptyValue("c");
        //a,b,c
        String result = add.toString();
        System.out.println(result);
        Assert.assertEquals("a,b,c", result);
        //或者
        String join = String.join("-", "a", "b", "c");
        System.out.println(join);
    }
    //有值+有前缀和后缀
    @Test
    public void test4(){
        StringJoiner joiner = new StringJoiner(",","{","}");
        StringJoiner add = joiner.add("a").add("b").add("c");
        //{a,b,c}
        String result = add.toString();
        System.out.println(result);
        Assert.assertEquals("{a,b,c}", result);
    }




    //  setEmptyValue()
    @Test
    public void test5(){
        StringJoiner joiner = new StringJoiner(",");
        //仅当StringJoiner是空才返回缺省值
        joiner.setEmptyValue("default").add("abc");
        String result = joiner.toString();
        //default  3
        System.out.println(result);
        System.out.println(result.length());
    }
    @Test
    public void test6(){
        StringJoiner joiner = new StringJoiner(",","{","}");
        joiner.setEmptyValue("default").add("a");
        String result = joiner.toString();
        //{}  3
        System.out.println(result);
        System.out.println(result.length());
    }
    public static String PREFIX="{";
    public static String SUFFIX="}";
    //merge()
    @Test
    public void test7(){
        StringJoiner joiner = new StringJoiner(",",PREFIX,SUFFIX);
        joiner.add("a").add("b").add("c");

        StringJoiner joiner2 = new StringJoiner("-",PREFIX,SUFFIX);
        joiner2.add("1").add("2").add("3");

        String result = joiner.merge(joiner2).toString();
        //{a,b,c,1-2-3}
        System.out.println(result);
    }

    //集合元素加分隔符
    @Test
    public void test8(){
        List<String> list = Arrays.asList("a", "b", "c");
        String collect = list.stream()
                .collect(Collectors.joining("#"));
        //{a,b,c,1-2-3}
        System.out.println(collect);

        //或者采用遍历的方法
        StringJoiner joiner = new StringJoiner("#");
        for (String s : list) {
            joiner.add(s);
        }
        System.out.println(joiner.toString());
    }

}
